﻿using System;
using System.Windows.Forms;
using Microsoft.Office.Interop.Outlook;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using Outlook.WorkItems.Extension.Manager.TeamProjectManager;

namespace Outlook.WorkItems.Extension.Addin.Forms
{
    public partial class MoveMailForm : Form
    {
        private readonly MAPIFolder _fromFolder;

        public MoveMailForm()
        {
            InitializeComponent();
        }

        public MoveMailForm(MAPIFolder fromMapiFolder)
        {
            InitializeComponent();

            _fromFolder = fromMapiFolder;
            try
            {
                var folder = fromMapiFolder;
                while (folder.Parent != null && folder.Parent is MAPIFolder)
                {
                    folder = folder.Parent as MAPIFolder;
                }

                //MAPIFolder item = Globals.ThisAddIn.Application.Inspectors.Session.GetDefaultFolder(OlDefaultFolders.olFolderInbox);
                MAPIFolder item = folder;

                TreeNode tn = null;

                if (item.DefaultItemType == OlItemType.olMailItem)
                {
                    tn = new TreeNode(item.Name);
                    tn.Tag = item;
                    treeView1.Nodes.Add(tn);
                }
                LoadSubFolders(item, tn);
            }
            catch { }
            TeamProjectLink tpl = TeamProjectLinksManager.GetTeamProjectLink(_fromFolder.EntryID);
            if (tpl != null)
            {
                TfsTeamProjectCollection tfs = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri(tpl.TeamServerUri));
                WorkItemStore wis = tfs.GetService<WorkItemStore>();

                foreach (WorkItemType wit in wis.Projects[tpl.TeamProjectName].WorkItemTypes)
                {
                    foreach (string value in wit.FieldDefinitions["State"].AllowedValues)
                    {
                        if (!comboBox2.Items.Contains(value))
                            comboBox2.Items.Add(value);
                    }
                }
            }
        }

        private void LoadSubFolders(MAPIFolder item, TreeNode parentNode)
        {
            foreach (var obj in item.Folders)
            {
                try
                {
                    MAPIFolder subItem = obj as MAPIFolder;
                    if (subItem == null)
                        return;

                    TreeNode tn = null;
                    if (subItem.DefaultItemType == OlItemType.olMailItem)
                    {
                        tn = new TreeNode(subItem.Name);
                        tn.Tag = subItem;
                        parentNode.Nodes.Add(tn);
                    }
                    LoadSubFolders(subItem, tn);
                }
                catch { }
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            TreeNode node = treeView1.SelectedNode;
            if (node != null && node.Tag is MAPIFolder)
            {
                MAPIFolder destinationFolder = node.Tag as MAPIFolder;

                TeamProjectLinksManager.MoveMailAssociatedToWorkItems(_fromFolder, destinationFolder, comboBox2.SelectedItem.ToString());
            }
            Close();
        }
    }
}
